﻿Namespace DB

    Public Enum DBFieldType
        DBnumber
        DBdate
        DBvarchar
        DBchar
        unknown
    End Enum

    Public Class DBField
        Implements System.ComponentModel.INotifyPropertyChanged

        Private _DBFieldID As Integer = -1
        Private _DBFieldName As String = ""
        Private _DBFieldType As DBFieldType = DBfieldType.DBnumber
        Private _DBFieldLength As Integer = 38
        Private _DBFieldDefault As String
        Private _DBFieldNullable As Boolean = True

        Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

        Public Shared Function FieldTypeToString(ByVal FieldType As DBFieldType) As String
            Select Case FieldType
                Case DBfieldType.DBnumber
                    Return "NUMBER"
                Case DBfieldType.DBchar
                    Return "CHAR"
                Case DBfieldType.DBvarchar
                    Return "VARCHAR2"
                Case DBfieldType.DBdate
                    Return "DATE"
                Case Else
                    Return ""
            End Select
        End Function

        Public Shared Function StringToFieldType(ByVal FieldTypeString As String) As DBFieldType
            Select Case FieldTypeString.ToUpper
                Case "NUMBER"
                    Return DB.DBFieldType.DBnumber
                Case "CHAR"
                    Return DB.DBFieldType.DBchar
                Case "VARCHAR2"
                    Return DB.DBFieldType.DBvarchar
                Case "DATE"
                    Return DB.DBFieldType.DBdate
                Case Else
                    Return DB.DBFieldType.unknown
            End Select
        End Function


        Public Property DBfieldID() As Integer
            Get
                Return _DBFieldID
            End Get
            Set(ByVal value As Integer)
                _DBFieldID = value
                RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("DBfieldID"))
            End Set
        End Property

        Public Property DBfieldName() As String
            Get
                Return _DBFieldName
            End Get
            Set(ByVal value As String)
                _DBFieldName = value.ToUpper
                RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("DBfieldName"))
            End Set
        End Property

        Public Property DBfieldType() As DBFieldType
            Get
                Return _DBFieldType
            End Get
            Set(ByVal value As DBFieldType)
                _DBFieldType = value

                If _DBFieldType = DB.DBFieldType.DBdate Then
                    _DBFieldLength = 0
                ElseIf _DBFieldType = DB.DBFieldType.DBnumber AndAlso _DBFieldLength > 38 Then
                    _DBFieldLength = 38
                ElseIf _DBFieldType = DB.DBFieldType.DBchar AndAlso _DBFieldLength = 0 Then
                    _DBFieldLength = 1
                ElseIf _DBFieldType = DB.DBFieldType.DBvarchar AndAlso _DBFieldLength = 0 Then
                    _DBFieldLength = 64
                End If

                RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("DBfieldType"))
                RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("DBfieldTypeAsString"))
                RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("DBfieldLength"))
            End Set
        End Property

        Public Property DBfieldTypeAsString() As String
            Get
                Return FieldTypeToString(_DBFieldType)
            End Get
            Set(ByVal value As String)
                DBfieldType = StringToFieldType(value)
            End Set
        End Property

        Public Property DBfieldLength() As Integer
            Get
                Return _DBFieldLength
            End Get
            Set(ByVal value As Integer)
                If _DBFieldType = DB.DBFieldType.DBnumber Then
                    _DBFieldLength = Math.Max(0, Math.Min(value, 38))
                ElseIf _DBFieldType = DB.DBFieldType.DBdate Then
                    _DBFieldLength = 0
                ElseIf _DBFieldType = DB.DBFieldType.DBchar Then
                    _DBFieldLength = Math.Max(1, Math.Min(value, 255))
                ElseIf _DBFieldType = DB.DBFieldType.DBvarchar Then
                    _DBFieldLength = Math.Max(1, Math.Min(value, 4000))
                End If

                RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("DBfieldLength"))
            End Set
        End Property

        Public Property DBfieldDefault() As String
            Get
                Return _DBFieldDefault
            End Get
            Set(ByVal value As String)
                _DBFieldDefault = value
                RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("DBfieldDefault"))
            End Set
        End Property

        Public Property DBfieldNullable() As Boolean
            Get
                Return _DBFieldNullable
            End Get
            Set(ByVal value As Boolean)
                _DBFieldNullable = value
                RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("DBfieldNullable"))
            End Set
        End Property

        Public Overrides Function ToString() As String
            Return _DBFieldName
        End Function

        Public Function Clone() As DBField
            Dim tmpField As New DBField
            tmpField.DBfieldDefault = Me.DBfieldDefault
            tmpField.DBfieldID = Me.DBfieldID
            tmpField.DBfieldLength = Me.DBfieldLength
            tmpField.DBfieldName = Me.DBfieldName
            tmpField.DBfieldNullable = Me.DBfieldNullable
            tmpField.DBfieldType = Me.DBfieldType
            Return tmpField
        End Function


    End Class
End Namespace

